# Adogs
# 时间: 2021/8/15 12:50
import pygame

from actor import ShootDir
from dialog.tunnel_dialog import TunnelTask4BeginDialog, TunnelTask4EndDialog, TunnelTaskFailDialog
from game import SCREEN_WIDTH
from scene.task import BaseTask, TaskStatus


class ShootTask(BaseTask):
    """
    任务四:射杀日本人
    方法：
    1.__init__(xiao_tie, japanese_group, font)
    2.do_self_task(key_down, key_list)
    """

    def __init__(self, xiao_tie, japanese_group, font):
        """
        初始化函数
        :param xiao_tie: 主角人物
        :param japanese_group: 日本人组
        :param font: 字体
        """
        self.japanese_group = japanese_group
        self.xiao_tie = xiao_tie
        # 加载死亡音效
        self.death_sound = pygame.mixer.Sound("resources/sound/死亡音效.mp3")
        self.death_sound.set_volume(0.3)

        self.score = 0  # 任务分数

        self.start_dialog = TunnelTask4BeginDialog(font)
        self.win_dialog = TunnelTask4EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        super(ShootTask, self).__init__(self.start_dialog, self.win_dialog, self.fail_dialog)

    def do_self_task(self, key_down, key_list):
        """
        任务进行函数
        :param key_down: 判断是否点击按键
        :param key_list: 判断是否有键按下
        :return: 无返回值
        """
        if self.score >= 6 or key_list[pygame.K_z]:  # 任务成功条件,杀死六个日本人即任务成功
            self.task_status = TaskStatus.win_dialog

        shoot_dir = self.xiao_tie.army_shoot_action.shoot_action.shoot_dir
        if shoot_dir != ShootDir.not_shoot:     # 当小铁在射击时
            for japanese in self.japanese_group:

                if shoot_dir == ShootDir.down:
                    if (japanese.pos_y >= self.xiao_tie.pos_y) and \
                            (abs(japanese.pos_x - self.xiao_tie.pos_x) <= 20):      # 判断日本士兵位置
                        self.japanese_group.remove(japanese)
                        self.score += 1
                        self.death_sound.play(0, 1000)      # 播放死亡音效,只播放一次,播放一秒

                elif shoot_dir == ShootDir.up:
                    if (japanese.pos_y <= self.xiao_tie.pos_y) and \
                            (abs(japanese.pos_x - self.xiao_tie.pos_x) <= 20):
                        self.japanese_group.remove(japanese)
                        self.score += 1
                        self.death_sound.play(0, 1000)

                elif (shoot_dir == ShootDir.right_down) or (shoot_dir == ShootDir.right_up):
                    if ((japanese.pos_x >= self.xiao_tie.pos_x) and (
                            abs(japanese.pos_x - self.xiao_tie.pos_x) <= SCREEN_WIDTH)) and \
                            (abs(japanese.pos_y - self.xiao_tie.pos_y) <= 20):
                        self.japanese_group.remove(japanese)
                        self.score += 1
                        self.death_sound.play(0, 1000)

                elif (shoot_dir == ShootDir.left_down) or (shoot_dir == ShootDir.left_up):
                    if ((japanese.pos_x <= self.xiao_tie.pos_x) and (
                            abs(japanese.pos_x - self.xiao_tie.pos_x) <= SCREEN_WIDTH)) and \
                            (abs(japanese.pos_y - self.xiao_tie.pos_y) <= 20):
                        self.japanese_group.remove(japanese)
                        self.score += 1
                        self.death_sound.play(0, 1000)
